﻿using System;
using QLNet;
using p = FinPlusAnalytics.QLConvParser;
using u = FinPlusUtility.Utility;

namespace FinPlusAnalytics
{
    public class BasisSwapTrade : Trade
    {
        public string Id { get; private set; }
       
        //construct
        public BasisSwapTrade(string marketName, string cacheName, string id, double nominal, DateTime start, DateTime maturity, string payRec, string curveName1, string index1, 
            double spread1, string fltLegFrq1, string fltLegConv1, string fltLegDayCount1, string curveName2, string index2, double spread2, string fltLegFrq2, 
            string fltLegConv2, string fltLegDayCount2, string holidays, bool endOfMonth = false)
        {
            Id = id;
            var market = Markets.Instance.GetMarket(marketName);
            var cache = Caches.Instance.GetCache(cacheName);
            var calendar = p.Calendar(holidays);

            var indx1 = market.GetIndex(index1);
            var indx2 = market.GetIndex(index2);
            var type = u.EnumParse<BasisSwap.Type>(p.PayRec(payRec));

		    var schedule1 = new Schedule(start, maturity, new Period(p.Freq(fltLegFrq1)), calendar, p.BizConv(fltLegConv1), 
                p.BizConv(fltLegConv1), DateGeneration.Rule.Forward, endOfMonth);
		
            var schedule2 = new Schedule(start, maturity, new Period(p.Freq(fltLegFrq2)), calendar, p.BizConv(fltLegConv2),
                p.BizConv(fltLegConv2), DateGeneration.Rule.Forward, endOfMonth);
		
			Underlying = new BasisSwap(type, nominal, schedule1, indx1,  spread1, p.DayCount(fltLegDayCount1), 
			 						 schedule2, indx2,  spread2, p.DayCount(fltLegDayCount2));

            cache.Add(id, this, curveName1);
        }
    }
}
